﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MethodWorx.CMS.Configuration;

namespace MethodWorx.CMS.Plugins
{
    public class PropertyDetails<T> : IPropertyDetails
    {
        [Required(ErrorMessage="Name is required")]
        [RegularExpression("^[a-zA-Z0-9]*$", ErrorMessage="Name must be alphanumeric without spaces")]
        public string Name { get; set; }
        public bool Mandatory { get; set; }
        public string Type { get; set; }
        public int Index { get; set; }
        public T Custom { get; set; }
        public string Group { get; set; }

        public object Cast(Type type)
        {
            var temp = Activator.CreateInstance(typeof(MethodWorx.CMS.Plugins.PropertyDetails<>).MakeGenericType(type));

            temp.GetType().InvokeMember("Index", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { this.Index });
            temp.GetType().InvokeMember("Name", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { this.Name });
            temp.GetType().InvokeMember("Mandatory", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { this.Mandatory });
            temp.GetType().InvokeMember("Type", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { this.Type });

            if (this.Custom != null)
            {
                temp.GetType().InvokeMember("Custom", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { this.Custom });

            }
            return temp;
        }

        object IPropertyDetails.Custom
        {
            get { return (ICustomPropertyDetails)this.Custom; }
            set { this.Custom = (T)value; }
        }
    }
}